#include "logicimpl.h"
#include "mainwindow.h"
#include <ffmpeg/ffmpeg_impl.h>
#include <ClientImpl/TcpClient.h>
#include <StreamBuf/StreamBuf.h>

LogicImpl::LogicImpl(MainWindow  * pWind)
{
   p_mpeg_impl   = new FFMPEG_Impl;
   p_tcpSocket   = new CTcpClient;
   p_Wind_buf    = pWind;
}

LogicImpl::~LogicImpl()
{
    p_mpeg_impl->colse();
    delete p_mpeg_impl;
    p_tcpSocket->DisConnect();
    delete p_tcpSocket;
}

void LogicImpl::Init()
{
    p_mpeg_impl->Init(640,480);
    p_tcpSocket->recv_handler(this);
    p_tcpSocket->Connet("182.92.177.100",9888);
}

bool LogicImpl::receive_handler(char *data, int size)
{
    qDebug(QString("shou dao size:").append(QString::number((long)size)).toLocal8Bit().data());
    int frameFinished;

     int nBufferLen =640*480*3;

    unsigned char *pBuffer = new unsigned char[nBufferLen];

    memset(pBuffer,0,nBufferLen);

    p_mpeg_impl->decode_video(frameFinished,(unsigned char *)data,size,pBuffer,nBufferLen);

    if((frameFinished)&&(nBufferLen>0))
    {
        if(p_Wind_buf)
            p_Wind_buf->add_rgb_buf(CStreamBuf(pBuffer,nBufferLen));
    }

    delete[]pBuffer;

    return true;
}

